package mediator;

import mumber.Zh0202Flight;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author: zh
 * @date: 2021/12/16 15:46
 * @version: 1.0
 */
public class Zh0202ATCMediator extends Zh0202Mediator {
    private Map<Zh0202Flight, String> flights = new HashMap<Zh0202Flight, String>();
    private Map<String, String> terminals = new HashMap<String, String>();

    //定义四个航站楼以供降落飞机
    public Zh0202ATCMediator() {
        terminals.put("t1", "null");
        terminals.put("t2", "null");
        terminals.put("t3", "null");
        terminals.put("t4", "null");
    }

    @Override
    public void addFlight(Zh0202Flight flight) {
        flights.put(flight, "null");
    }

    @Override
    public void sendMessage(HashMap map, String terminal) {
        Iterator iter = map.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            Object key = entry.getKey();
            Zh0202Flight zh0202Flight = (Zh0202Flight) key;
        }
    }

    @Override
    public void receiveMessage(Zh0202Flight flight, String terminal) {
        if (terminals.get(terminal) == "null") {
            flights.remove(flight);
            terminals.put(terminal, flight.toString());
            this.sendMessage((HashMap) flights, terminal);
            flights.put(flight, terminal);
            System.out.println(flight.toString() + "申请" + terminal + "成功");
        } else {
            flight.receiveMessage("该航站楼已经有飞机停落，请选择其他航站楼，谢谢配合！");
        }
    }
}
